﻿using System;
using System.Collections.Generic;
using DataBaseConnect.IRepositories;
using Entity;
using Utils;

namespace DataBaseConnect.App
{
    public class FormAppService : IFormAppService
    {
        private readonly IFormRepository _formRepository;

        public FormAppService(IFormRepository formRepository)
        {
            _formRepository = formRepository;
        }

        /// <summary>
        /// 新增表单
        /// </summary>
        /// <param name="Input">参数实体</param>
        /// <returns></returns>
        public ReceiveStatus AddFromMsg(Form Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _formRepository.Insert(Input, true);
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 获取所有的表单和根据表单名称查询
        /// </summary>
        /// <param name="Name">表单名称</param>
        /// <returns></returns>
        public List<Form> GetFormList(string Name)
        {
            if (!string.IsNullOrEmpty(Name))
            {
                return _formRepository.GetAllList(f => f.Name == Name);
            }
            else
            {
                return _formRepository.GetAllList();
            }
        }

        /// <summary>
        /// 根据主键获取表单
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public List<Form> GetFormId(int Id)
        {
            return _formRepository.GetAllList(f => f.Id == Id);
        }

        /// <summary>
        /// 编辑表单信息
        /// </summary>
        /// <param name="Input">参数实体</param>
        /// <returns></returns>
        public ReceiveStatus EditFormMsg(Form Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _formRepository.Update(Input);
                receiveStatus.msg = "编辑成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "编辑失败：" + ex.Message;
            }
            return receiveStatus;
        }
    }
}
